// pages/common-page/xm-shop-prod/index.ts
import { getProductList, searchProduct, getSKUList } from '../../../api/index'
const hideScrollBar = require("../../../behavior/hide-scroll-bar");
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [hideScrollBar],
  data: {
    pageHeight: app.globalData.pageHeight,
    activeKey: 0,
    prodList: [],
    pageConfig: {
      "Status": 5,
      "Page": 1,
      "PageSize": 20,
      "NeedEditSpu": 0
    },
    searchConfig: {
      "Keyword": "",
      "Source": 0,
      "Page": 0,
      "PageSize": 0
    },
    searchVal: "",
    selectProductIds: [] as any
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    this.getGoodsList().then(() => {
      if (e.ids && e.ids.length > 0) {
        const ls = e.ids.split(",").map(Number)
        let prodList: any = this.data.prodList;
        ls.forEach(id => {
          const index = prodList.findIndex((ele: any) => ele.product_id == id)
          prodList[index].isSelect = true;
        });
        this.setData({
          selectProductIds: ls,
          prodList
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getGoodsList() {
    return new Promise((resolve, reject) => {
      if (this.data.searchVal == '') {
        getProductList(this.data.pageConfig).then(res => {
          let prodList: any = this.data.prodList;
          prodList = res.Data.SPus.map((ele) => ({ ...ele, isSelect: this.data.selectProductIds.includes(ele.product_id) }));

          prodList.forEach(element => {
            let min_price = String(element.min_price);
            element["price"] = `${min_price.slice(0, min_price.length - 2)}.${min_price.slice(min_price.length - 2, min_price.length)}`
          });
          console.log(prodList);

          this.setData({
            prodList
          })
          return prodList.map(e => e.product_id)
        }).then((product_ids) => {
          // product_ids.forEach(id => {
          //   getSKUList({ "ProductId": id, "NeedEditSku": 0, "NeedRealStock": 1 }).then(res => {
          //     console.log(res);

          //   })
          // });
          resolve(product_ids)
        })
      } else {
        let searchConfig = this.data.searchConfig;
        searchConfig.Keyword = this.data.searchVal;
        console.log(searchConfig);

        searchProduct(searchConfig).then(res => {
          // let prodList: any = this.data.prodList;
          console.log(res);
          resolve(res)
        })

      }
    })
  },
  searchValChange(event) {
    this.setData({ searchVal: event.detail })
    this.getGoodsList()
  },
  toProductDetail(event) {
    const { product_id } = event.currentTarget.dataset
    wx.navigateTo({
      url: `plugin-private://wx34345ae5855f892d/pages/productDetail/productDetail?productId=${product_id}`,
    });
  },
  correlation(event) {
    console.log("关联");
    const { product_id } = event.currentTarget.dataset;
    let selectProductIds = this.data.selectProductIds;
    let prodList = this.data.prodList;
    const index = selectProductIds.indexOf(product_id);
    if (index != -1) {
      selectProductIds.splice(index, 1)
    } else {
      selectProductIds.push(product_id)
    }
    const prodIndex = prodList.findIndex(ele => ele.product_id == product_id)
    prodList[prodIndex].isSelect = !prodList[prodIndex].isSelect
    this.setData({ selectProductIds, prodList })
  },
  goBackPage() {
    const pages = getCurrentPages();
    const lastPage = pages[pages.length - 2]
    lastPage.setData({
      SelfGoodsIds: this.data.selectProductIds,
      ADVId: '',
      adValue: ''
    })
    wx.navigateBack()
  }
})
export { }